package com.ljz.servlet;

import com.ljz.dao.EmpDao;
import com.ljz.entity.Employ;
import com.ljz.utils.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/addEmp")
public class AddEmpServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        SqlSession sqlSession = SqlSessionFactoryUtil.getSqlSession();
        EmpDao empDao = sqlSession.getMapper(EmpDao.class);
        Employ emp = new Employ();
        emp.setEname(req.getParameter("ename"));
        emp.setSalary(Double.valueOf(req.getParameter("salary")));
        emp.setWorkDate(req.getParameter("workDate"));
        emp.setDeptid(Integer.valueOf(req.getParameter("deptid")));
        emp.setEsex(req.getParameter("esex"));
        System.out.println(emp);
        int result = empDao.addEmp(emp);
        sqlSession.commit();
        sqlSession.close();
        PrintWriter out = resp.getWriter();
        resp.setContentType("text/html;charset=utf-8");
        if (result > 0) {
            out.print("true");
        } else {
            out.print("false");
        }

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
